package main

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

var (
	ans  int
	path []int
)

func pseudoPalindromicPaths(root *TreeNode) int {
	preOrder(root, false)
	return ans
}

func preOrder(root *TreeNode, is bool) {
	if root == nil {
		if is {
			ans++
		}
		return
	}

	path = append(path, root.Val)
	if !is {
		is = isp(path)
	}

	preOrder(root.Left, is)
	preOrder(root.Right, is)
	path = path[:len(path)-1]
}

func isp(path []int) bool {
	for i := range path {
		var m = true
		for j, k := i, 0; j < (j+len(path))/2; j++ {
			if path[j] != path[len(path)-1-k] {
				m = false
			}
			k++
		}
		if m {
			return m
		}
	}
	return false
}
